package cn.bone.stress.config;

import cn.bone.stress.bean.GlobalParams;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by superbone on 2017/3/28.
 */
@Configuration
public class MongoConfig {

    @Resource
    private GlobalParams globalParams;

    @Bean
    public MongoClient mongoClient(){

        MongoClientURI mongoClientURI = new MongoClientURI(globalParams.getMongo().getUrl());
        MongoClient client = new MongoClient(mongoClientURI);
        return client;
    }

    @Bean
    public Datastore datastore(){
        Morphia morphia = new Morphia();
        morphia.mapPackage("cn.bone.**");
        //如果要使实体类上的index注解有效,这里要手动使用
        //morphia.mapPackageFromClass(Xxx.class)

        MongoClient client = mongoClient();
        Datastore datastore = morphia.createDatastore(client,globalParams.getMongo().getDbName());
        return datastore;
    }
}
